//
//  ShellMineViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/5/15.
//  Copyright © 2019 吕VV. All rights reserved.
//

#import "ShellMineViewModel.h"

@implementation ShellMineViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
       
        
    }
    return self;
}


- (void)getMineData{
    
    [self.dataArray removeAllObjects];
    
    NSArray *titles = @[@"邀请节点",@"全球汇率",@"登录密码",@"交易密码",@"世界语言",@"主网系统",@"TRITON LAB"];
    NSArray *imageNames = @[@"wodehaoyou",@"quanqiuhuilv",@"denglumima",@"jiaoyimima",@"yuyan",@"dangqianbanben",@"guanyuwomen"];
    for (int i = 0; i < titles.count; i++){
        ShellMineModel *mineModel = [[ShellMineModel alloc] init];
        mineModel.title = LZLocalizedString(titles[i]);
        if(i == titles.count - 2){
            
            NSString *localVerson = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
            NSString *build =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

            mineModel.detail = [NSString stringWithFormat:@"V%@(%@)",localVerson,build];
        }
        mineModel.imageName = imageNames[i];
        [self.dataArray addObject:mineModel];
    }
    [self.reloadSubject sendNext:@(1)];
}

//获取个人信息
- (void)getUserInfo{
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kUserInfo params:nil success:^(id  _Nullable responseObject) {
        @strongify(self);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            NSDictionary *dataDictionary = responseObject[kResultData];
            ShellUserDetailModel *infoModel = [LZToolCache getUserInfo];
            infoModel.name = dataDictionary[@"name"];
            infoModel.email = dataDictionary[@"email"];
            infoModel.portrait = dataDictionary[@"portrait"];
            infoModel.myinviteCode = dataDictionary[@"myinviteCode"];
            infoModel.showLevel = dataDictionary[@"showLevel"];
            [LZToolCache saveUserInfo:infoModel];
        }

        [self.reloadSubject sendNext:@(2)];

    } failure:^(NSError * _Nullable error) {}];
  
}

// 获取通知消息
- (void)getUserNotification{
    
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kUnReadeMsg params:nil success:^(id  _Nullable responseObject) {
        @strongify(self);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            self.unReadNumber = [responseObject[kResultData] integerValue];
        }
        [self.reloadSubject sendNext:@(2)];

    } failure:^(NSError * _Nullable error) {}];
}



/**
 Convert 修改个人信息
 */
- (void)updateAccountInfo:(NSDictionary *)dataDictioany{
    
    [LZNetworkingManager lz_request:@"post" url:kUploadPortrait params:dataDictioany success:^(id  _Nullable responseObject) {
        dismiss(nil);
 
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
            detailModel.portrait = dataDictioany[@"portrait"];
            [LZToolCache saveUserInfo:detailModel];
        }
    
        [[NSNotificationCenter defaultCenter] postNotificationName:k_APPUserInfoWillChangeNotificationName object:nil];
        
    } failure:^(NSError * _Nullable error) {} isFailTip:YES];
}


///上传头像
- (void)updateUserHeadImg:(UIImage *)image{
    
    NSDictionary *params = @{@"busiCode":@"portrait",
                             @"file":image};
    
    showLoadingTitle(@"", nil);
    @weakify(self);
    //此body是向后台传的参数, 因为是上传图片, 所以只给个图片名就够了, 这个和后台去问
    [LZNetworkingManager lz_postWithBaseUrl:kUploadImg params:params images:@[image] fileName:@"portrait" compressionRatio:0.5 success:^(id  _Nullable responseObject) {
        @strongify(self);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            [self updateAccountInfo:@{@"portrait":responseObject[kResultData],
                                      @"token":k_userToken}];
        } else {
            dismiss(nil);
            NSString *resultDesc = [NSString stringWithFormat:@"%@",responseObject[kResultMessage]];
            showDelayedDismissTitle(resultDesc, nil);
        }
        NSLog(@"LZNetworkingManager上传成功%@",responseObject);

    } failure:^(NSError * _Nullable error) { } notwork:^(NSError * _Nullable error) { }];
}


//我的等级
- (void)getLevelInfo{
    
    [LZNetworkingManager lz_request:@"get" url:kLevelInfo params:@{} success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            self.levelDictionary = [[NSMutableDictionary alloc] initWithDictionary:responseObject[kResultData]];
               
            NSInteger level = [self.levelDictionary[@"level"] integerValue];
            NSString *levelString = @"";
            if (level < 5) {
                levelString = [NSString stringWithFormat:@"B lv%ld",level];
            } else {
                levelString = [NSString stringWithFormat:@"S lv%ld",level-10];
            }
            
            self.levelDictionary[@"levelString"] =levelString;
            [self.reloadSubject sendNext:@(2)];
        }
     } failure:^(NSError * _Nullable error) {} isFailTip:NO];
    
    [self getUserInfo];
}

@end
